package com.yanftch.hdc_tools.command

import com.yanftch.hdc_tools.common.MessageDelegate
import com.yanftch.hdc_tools.common.executeCommand
import com.yanftch.hdc_tools.common.finalCommandLine
import com.yanftch.hdc_tools.common.isCommandSucceed

class UnInstallCommand(private val withHSP: Boolean) : Command {
    override fun run(commandContext: CommandContext): Boolean {
        with(commandContext) {
            try {
                val realCommand = if (withHSP) finalCommandLine(hdcPath, hdc_uninstall_app_with_hsp, bundleName) else finalCommandLine(hdcPath, hdc_uninstall_app, bundleName)

                val executeCommand = executeCommand(realCommand)
                if (executeCommand.isCommandSucceed()) {
                    // https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/hdc-V5#%E5%BA%94%E7%94%A8%E7%9B%B8%E5%85%B3%E5%91%BD%E4%BB%A4
                    MessageDelegate.info("bundleName: $bundleName 卸载成功", action)
                    return true
                } else {
                    MessageDelegate.error("bundleName: $bundleName 卸载失败", action)
                    return false
                }
            } catch (e: Exception) {
                MessageDelegate.error("UnInstallCommand error: ${e.message}", action)
            }
        }
        return false
    }
}